package com.yanqu.road.server.manger.activity.silkroad.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.dao.impl.player.UserSilkRoadDaoImpl;
import com.yanqu.road.entity.activity.silkroad.UserSilkRoadData;
import com.yanqu.road.pb.activity.SilkRoadProto;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.silkroad.SilkRoadModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.Map;

public class SilkRoadLookOtherReflicServiceCmd implements IGameServiceCommand {
    @Override
    public SyncResponse execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncResponse syncResponse = new SyncResponse();
        SilkRoadProto.SilkRoadLookOtherRelicReqMsg reqMsg = SilkRoadProto.SilkRoadLookOtherRelicReqMsg.parseFrom(request.getDataBytes());
        GamePlayer targetPlayer = GamePlayerMgr.getOnlinePlayer(reqMsg.getUserId());
        UserSilkRoadData userData = null;
        if (targetPlayer == null) {
            userData = new UserSilkRoadDaoImpl().getUserSilkRoadData(reqMsg.getUserId(), reqMsg.getActivityId());
        } else {
            userData = targetPlayer.getModule(SilkRoadModule.class).getUserData();
        }
        SilkRoadProto.SildRoadLookOtherRelicRespMsg.Builder resp = SilkRoadProto.SildRoadLookOtherRelicRespMsg.newBuilder();
        resp.setRet(0);
        if (userData != null) {
            resp.setAchievement(userData.getAchievement());
            for (Map.Entry<Integer, Integer> entry : userData.getRefloatItemMap().entrySet()) {
                SilkRoadProto.SilkRoadRelicTemp.Builder relicsBuilder = SilkRoadProto.SilkRoadRelicTemp.newBuilder();
                relicsBuilder.setRelicId(entry.getKey());
                relicsBuilder.setNum(entry.getValue());

                resp.addRelics(relicsBuilder);
            }
        } else {
            resp.setRet(GameErrorCode.E_ERROR_DATA);
        }
        syncResponse.setResponseBuilder(resp);
        return syncResponse;
    }
}
